// for Emacs -*- c++ -*-


private:

// the message queue (owned by the EventProcessor)
MultiMessageQueue msgQueue;

// mapping of message types to processing functions
typedef map< int, msgProcessor > ProcessorMap;
ProcessorMap processorsMap;


// keep track of thread structures started to stop threads in destructor
vector< pthread_t* > vThreads;

// variable to keep track how many threads exited the Spin()i
// needed to determine when it is safe to signal the processes waiting j
// to know when the EventProcessor died
int finishedSpins;

// bool variable set by the Die message to cleanly finish the spin of the
// thread executing it
// declared volatile so that the compiler does not optimize it
// it could change at any time
volatile bool exitSpin;

///////////
// Facilities to wait for the event processor to die

pthread_cond_t died; // conditional variable that signals the death of the event processor
pthread_mutex_t diedMtx; // the mutex required for the variable to work

/////////////
// Helper functions

// helper function to start a thread running Spin()
// the argument passed is the pointer to the EventProcessor on which Spin is executed
static void* ForkAndSpinThread(void*);


// Start the event processing loop. This goes forever but does not do 
// busy wayting
// everybody should use ForkAndSpin instead of Spin to start a thread running the Spin()
void Spin(void);

protected:

// data for debuging purposes
bool debug;
char *dbgMsg;

// Processsor for the kill messages
// When this method is called, it should be assumend that 
// the object cannot be accessed further (except the destructor)
static void processDieMessage(EventProcessor &_obj, Message &in);

// some event processors need to be cloned so that multiple threads work in parallel
// this has to be done carefully so by default no event processor is forkable
int forksRemaining; // set to 1 so only one thread runs

// general purpose mutex to be used in this class. More refined mutexes can be defined 
// and used in derived classes
pthread_mutex_t mutex;


// register a processor for a new type of message
// the Type is the numeric type associated with the type of message
// proc is the method dealing with this type of message
// Priority is the priority of this type (default highest)
// The constructor of any derived class that has clear functionality has to call this method for all the messages tha need to be processed
// Only designers of Event Processors should call this otherwise it can be quite dangerous
// If the method is called multiple times for the same type, the old info is overwritten
// this allows "overloading" of the Die behavior
void RegisterMessageProcessor(int Type, msgProcessor Proc, int Priority=1);

/////////////////////////
// DISABLING COPYING
EventProcessor(EventProcessor&);
EventProcessor& operator =(const EventProcessor&);
